package boot.spring.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import boot.spring.mapper.SignApplyMapper;
import boot.spring.po.SignApply;
import boot.spring.service.SignService;

@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT,timeout=10)
@Service
public class SignServiceImpl implements SignService {
	
	@Autowired
	IdentityService identityservice;
	@Autowired
	RuntimeService runtimeservice;
	@Autowired
	TaskService taskservice;

	@Autowired
	SignApplyMapper signApplyMapper;
	@Override
	public ProcessInstance startWorkflow(SignApply apply, String userid, Map<String, Object> variables) {
		// TODO Auto-generated method stub
		apply.setApply_time(new Date().toString());
		apply.setUser_id(userid);
		signApplyMapper.save(apply);
		String businesskey=String.valueOf(apply.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
		identityservice.setAuthenticatedUserId(userid);
		ProcessInstance instance=runtimeservice.startProcessInstanceByKey("multiAssigneeProcess",businesskey,variables);
		System.out.println(businesskey);
		String instanceid=instance.getId();
		apply.setProcess_instance_id(instanceid);
		signApplyMapper.updateByPrimaryKey(apply);
		return instance;
	}
	@Override
	public List<SignApply> getpagedepttask(String userid, int firstrow, int rowcount) {
		List<SignApply> results=new ArrayList<SignApply>();
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("部门经理").listPage(firstrow, rowcount);
		for(Task task:tasks){
			String instanceid=task.getProcessInstanceId();
			ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
			String businesskey=ins.getBusinessKey();
			SignApply a=signApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
			a.setTask(task);
			results.add(a);
		}
		return results;
	}
	@Override
	public int getalldepttask(String userid) {
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("部门经理").list();
		return tasks.size();
	}
	@Override
	public SignApply getleave(int id) {
		SignApply leave=signApplyMapper.getLeaveApply(id);
		return leave;
	}
	@Override
	public List<SignApply> getpagecwtask(String userid, int firstrow, int rowcount) {
		List<SignApply> results=new ArrayList<SignApply>();
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("财务管理员").listPage(firstrow, rowcount);
		for(Task task:tasks){
			String instanceid=task.getProcessInstanceId();
			ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
			String businesskey=ins.getBusinessKey();
			SignApply a=signApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
			a.setTask(task);
			results.add(a);
		}
		return results;
	}
	@Override
	public int getallcwtask(String userid) {
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("财务管理员").list();
		return tasks.size();
	}
	@Override
	public List<SignApply> getpageMantask(String userid, int firstrow, int rowcount) {
		List<SignApply> results=new ArrayList<SignApply>();
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("总经理").listPage(firstrow, rowcount);
		for(Task task:tasks){
			String instanceid=task.getProcessInstanceId();
			ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
			String businesskey=ins.getBusinessKey();
			SignApply a=signApplyMapper.getLeaveApply(Integer.parseInt(businesskey));
			a.setTask(task);
			results.add(a);
		}
		return results;
	}
	@Override
	public int getallMantask(String userid) {
		List<Task> tasks=taskservice.createTaskQuery().taskCandidateGroup("总经理").list();
		return tasks.size();
	}
}
